package cn.edu.scau.cmi.yanweijie.client;

import java.util.HashSet;
import java.util.List;
import java.util.Scanner;

import javax.xml.transform.Source;

import cn.edu.scau.cmi.yanweijie.combination.Component;
import cn.edu.scau.cmi.yanweijie.combination.Organization;
import cn.edu.scau.cmi.yanweijie.combination.Person;
import sun.security.action.GetBooleanAction;

public class CombinationClient {
	static HashSet<String> organHashSet = new HashSet<String>();
	static HashSet<String> perHashSet = new HashSet<String>();
	static Organization organization = new Organization();
	
	public static void main(String[] args) {
		organization.setFatherName("源");
		organization.setName("草帽海贼团");
		Person person = new Person("路飞");
		Person person2 = new Person("布鲁克");
		Person person3 = new Person("罗宾");
		Person person4 = new Person("娜美");
		perHashSet.add(person.getName());
		perHashSet.add(person2.getName());
		perHashSet.add(person3.getName());
		perHashSet.add(person4.getName());
		Organization organization2 = new Organization();
		organization2.setName("女生小组");
		organHashSet.add(organization.getName());
		organHashSet.add(organization2.getName());

		//organization.addOrganization(organization);
		organization.addOrganization(organization2);
		organization.addPerson(person);
		organization.addPerson(person2);
		organization2.addPerson(person3);
		organization2.addPerson(person4);

		
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("输入1进行查看组织结构");
			System.out.println("输入2进行插入选择");
			System.out.println("输入3进行删除节点操作");
			System.out.println("输入4进行修改节点操作");
			
			String input = scanner.next();
			
			if(input.equalsIgnoreCase("1")) {
				stdOut(organization);
			}else if(input.equalsIgnoreCase("2")) {
				System.out.println("请输入你要添加位置的父节点名称：");
				add(scanner.next());
				System.out.println("\n     已经添加成功了");
				System.out.println("     可以输入1查看组织结构\n");
			}else if(input.equalsIgnoreCase("3")) {
				System.out.println("请输入你要删除的节点名称");
				delete(organization,scanner.next());
				System.out.println("删除成功");
			}else if(input.equalsIgnoreCase("4")) {
				System.out.println("请输入你要修改的节点名称以及新命名");
				System.out.print("旧节点名称：");
				String oldString = scanner.next();
				System.out.print("新节点名称：");
				String newString = scanner.next();
				change( oldString, newString);
				System.out.println("修改成功");
			}
			
		}
		
		
		
		//System.out.println(organization.toString());
		//stdOutput(person);
		//stdOutput(organization);
		//JSONObject 
	}
	
	public static void stdOutput(Person person) {
			String blankString = getBlank(person.getGeneration());
			String blankString2 = getBlank2(person.getGeneration());
			System.out.println(blankString+"{"+"\n"+blankString2+"    父亲："+person.getFatherName()+","+"\n"+blankString2+"    名字："+person.getName()+",\n"+blankString2+"    类型："+person.getType()+"\n"+blankString+"}");
	}
	
	public static void stdOutput(Organization organization) {
		String blankString = getBlank(organization.getGeneration());
		String blankString2 = getBlank2(organization.getGeneration());
		System.out.println(blankString+"{"+"\n"+blankString2+"    父亲："+organization.getFatherName()+","+"\n"+blankString2+"    名字："+organization.getName()+",\n"+blankString2+"    类型："+organization.getType()+"\n"+blankString+"}");
	}
	
	public static String getBlank(int a ) {
		String resultString ="";
		while (a-->=0) {
			resultString+="       ";
		}
		return resultString;
	}
	

	public static String getBlank2(int a ) {
		String resultString ="";
		while (a-->=0) {
			resultString+="           ";
		}
		return resultString;
	}
	
	public static void stdOut(Organization organization) {
		stdOutput(organization);
		List<Organization> orgList = organization.getOrgList();
		List<Person> perList = organization.getPerList();
		
		if (orgList==null||orgList.size()==0) {
			
		}
		for (Organization organization2 : orgList) {
			stdOut(organization2);
		}
		for (Person person : perList) {
			stdOutput(person);
		}
		
	}
	
	public static void add(String name1) {
		Scanner in=new Scanner(System.in);
		String hostString = name1;
		//System.out.println(hostString);
		
		if(organHashSet.contains(hostString));
		else {
			System.out.println("不存在此节点");
			return ;
		}
		
		System.out.println("如果添加的是个人，请输入per");
		
		System.out.println("如果添加的是组织，请输入org");
		String inputString = in.next();
		if(inputString.equalsIgnoreCase("per")) {
			System.out.print("请输入名称：");
			Person p = new Person(in.next());
			System.out.println("往节点:"+name1+" 下添加Person节点"+p.getName());
			addPer(organization,hostString, p);
		}else if (inputString.equalsIgnoreCase("org")) {
			System.out.println("请输入组织名称：");
			Organization newOrganization = new Organization();
			newOrganization.setName(in.next());
			System.out.println("往节点:"+name1+" 下添加Organization节点:"+newOrganization.getName());
			addOrg(organization, name1, newOrganization);
		}else {
			System.out.println("输入错误请重试");
		}
		
	}
	
	public static void addPer(Organization org,String name,Person person) {
		if (org.getName().equalsIgnoreCase(name)) {
			org.addPerson(person);
			perHashSet.add(person.getName());
			return ;
		}
		List<Organization> list = org.getOrgList();
		for (Organization org1 : list) {
			addPer(org1,name, person);
			//System.out.println(org1.getName()+"  "+name);
			if (org1.getName().equalsIgnoreCase(name)) {
				org1.addPerson(person);
				perHashSet.add(person.getName());
				return ;
			}
		}
	}
	public static void addOrg(Organization org,String name,Organization org1) {
		if (org.getName().equalsIgnoreCase(name)) {
			org.addOrganization(org1);
			organHashSet.add(org1.getName());
			return ;
		}
		
		List<Organization> list = org.getOrgList();
		for (Organization org2 : list) {
			addOrg(org2,name,org1);
			if (org2.getName().equalsIgnoreCase(name)) {
				org2.addOrganization(org1);
				organHashSet.add(org1.getName());
				return ;
			}
		}
	}
	
	public static void delete(Organization organization,String name) {
		if (!organHashSet.contains(name)&&!perHashSet.contains(name)) {
			System.out.println("不存在符合的节点");
			return ;
		}
		
		
		if(organHashSet.contains(name)) {
			//System.out.println("正在尝试删除组织");
			deleteOrg(organization, name);
			//System.out.println("delete compelete");
		}else if(perHashSet.contains(name)) {
			//System.out.println("正在尝试删除人");
			deletePer(organization, name);
			//System.out.println("delete action");
		}
		
		/*List<Organization> list = organization.getOrgList();
		if (list==null||list.size()==0) {
			
		}else {
		for (Organization organization2 : list) {
			delete(organization2, name);
			if (organization2.getName().equalsIgnoreCase(name)) {
				{organization.remove(organization.getName(), organization2.getName());
				System.out.println("成功移除");}
			}
		}
		}*/
	}
	
	public static void deletePer(Organization org,String name) {
		List<Organization>list = org.getOrgList();
		for (Organization organization : list) {
			deletePer(organization, name);
		}
		List<Person> list2 = org.getPerList();
		for (Person person : list2) {
			//System.out.println(person.getName()+"  "+name);
			if (person.getName().equalsIgnoreCase(name)) {
				org.remove(org.getName(), person.getName());
				return ;
			}
		}
	}
	public static void deleteOrg(Organization org,String name) {
		List<Organization>list = org.getOrgList();
		for (Organization organization : list) {
			deletePer(organization, name);
			if (organization.getName().equalsIgnoreCase(name)) {
				org.remove(org.getName(), name);
				return ;
			}
		}
	}
	public static void change(String oldName,String newName) {
		if (perHashSet.contains(oldName)) {
			System.out.println("修改Person信息");
			changePer(organization, oldName,newName);
		}else if(organHashSet.contains(oldName)) {
			System.out.println("修改Org信息");
			changeOrg(organization, oldName,newName);
		}else {
			System.out.println("输入有误");
		}
		return ;
	}
	
	public static void changePer(Organization organization,String oldName,String newName) {
		List<Person> list = organization.getPerList();
		List<Organization> orgList =organization.getOrgList();
		for (Organization organization2 : orgList) {
			changePer(organization2, oldName,newName);
		}
		for (Person person : list) {
			if (person.getName().equalsIgnoreCase(oldName)) {
				person.setName(newName);
				System.out.println("Per名称已修改成"+newName);
				return ;
			}
		}
		return ;
	}
	public static void changeOrg(Organization organization,String oldName,String newName) {
		List<Organization> list = organization.getOrgList();
		if (organization.getName().equalsIgnoreCase(oldName)) {
			organization.setName(newName);
			System.out.println("Org名称已被修改成"+newName);
			return ;
		}
		for (Organization organization2 : list) {
			changeOrg(organization2, oldName, newName);
			if (organization.getName().equalsIgnoreCase(oldName)) {
				organization.setName(newName);
				System.out.println("Org名称已被修改成"+newName);
				List<Person> persons = organization.getPerList();
				for (Person person : persons) {
					System.out.println("修改子节点名称");
					person.setFatherName(newName);
				}
			}
		}
	}
}
